package com.clouddot.baipai.ui.util;


import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import com.clouddot.baipai.infrastructure.environment.Env;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2018/4/10.
 */

public class getTimeAndWeatherUtil {

    static List<Map<String, String>> mapList = new ArrayList<>();

    public static String  getTimeAndData(){;
            DateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 | -HH:mm");
            Calendar c = Calendar.getInstance();
            final String format = formatter.format(c.getTime()) +'-'+ getWeekDay();
            return format;

    }

    private static String getWeekDay(){
        String Week = "星期";
        Calendar c = Calendar.getInstance();
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "六";
        }
        return Week;
    }



    public static List<Map<String,String>> getWeatherInfo(String s) throws DocumentException {
        mapList = new ArrayList<>();
        Document document = DocumentHelper.parseText(s);
        Element root = document.getRootElement();
        List<Element> elements = root.elements();
        for(Iterator<Element> it = elements.iterator();it.hasNext();) {
            analysWeather(it);
        }
        Env.info("获取天气预报");
        return mapList;
    }

    private static void analysWeather(Iterator<Element> it){
        Element element = it.next();
        List<Element> list = element.elements();
        if(list.size() > 0){
            for(Iterator<Element> t = list.iterator();t.hasNext();){
                analysWeather(t);
            }
        }
        Map<String,String> map = new HashMap<>();
        map.put(element.getName(),element.getTextTrim());
        mapList.add(map);
    }

    public static boolean isServiceRunning(final Activity activity ,final String className) {
        ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> info = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (info == null || info.size() == 0) return false;
        for (ActivityManager.RunningServiceInfo aInfo : info) {
            if (className.equals(aInfo.service.getClassName())) return true;
        }
        return false;
    }

}
